/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package main;

import java.io.IOException;
import java.io.InputStreamReader;
import org.kxml.Xml;
import org.kxml.parser.ParseEvent;
import org.kxml.parser.XmlParser;
import util.CommonUtil;

public class KamusParser {

    private XmlParser parser;
    private ParseEvent pe;

    /**
     * parsing xml response update file database dari server
     * @param result
     * @param content
     * @return
     * @throws IOException
     */
    public RespUpdate parseUpdate(String result,String [] content) throws IOException {
        RespUpdate update = new RespUpdate();
        parser = new XmlParser(new InputStreamReader(CommonUtil.stringToInputStream(result)));
        pe = null;
        pe = parser.read();

        while (pe.getType() != Xml.END_DOCUMENT) {
            pe = parser.read();
            if (pe.getType() == Xml.START_TAG) {
                if (pe.getName().equals("code")) {
                    pe = parser.read();
                    update.setCode(pe.getText());
                } else if (pe.getName().equals("msg")) {
                    pe = parser.read();
                    update.setMessage(pe.getText());
                } else if (pe.getName().equals("db")) {
                    String type = pe.getAttribute("name").getValue();
                    while (pe.getType() != Xml.END_TAG || pe.getName().equals("db") == false) {
                        pe = parser.read();
                        if (pe.getType() == Xml.START_TAG) {
                            if (pe.getName().equals("date")) {
                                pe = parser.read();
                                if (type.equals("gi")) {
                                    update.setContentGI(content[0]);
                                    update.setDateGI(pe.getText());
                                } else if (type.equals("ig")) {
                                    update.setContentIG(content[1]);
                                    update.setDateIG(pe.getText());
                                }
                            }
                        }
                    }
                }
            }
        }
        System.gc();
        return update;
    }

    /**
     * parsing response tambah kata dari server
     * @param result
     * @return
     * @throws IOException
     */
    public RespAddWord parseAddWord(String result) throws IOException {
        RespAddWord addWord = new RespAddWord();
        parser = new XmlParser(new InputStreamReader(CommonUtil.stringToInputStream(result)));
        pe = null;
        pe = parser.read();

        while (pe.getType() != Xml.END_DOCUMENT) {
            pe = parser.read();
            if (pe.getType() == Xml.START_TAG ) {
                if (pe.getName().equals("code")) {
                    pe = parser.read();
                    addWord.setCode(pe.getText());
                } else if (pe.getName().equals("msg")) {
                    pe = parser.read();
                    addWord.setMessage(pe.getText());
                }
            }
        }
        System.gc();
        return addWord;
    }
}
